﻿namespace FFF.Test.Util.Helper
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading.Tasks;

    /// <summary>
    /// 任务日志
    /// </summary>
    /// <remarks>于大明</remarks>
    public static class WorkerLogHelper
    {
        private static string _logPath = AppDomain.CurrentDomain.BaseDirectory + "JobLogs";

        /// <summary>
        /// 记录任务日志
        /// </summary>
        /// <param name="workName">Name of the job.</param>
        /// <param name="content">The content.</param>
        /// <remarks>于大明 2019-11-27</remarks>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public static async Task WriteLogAsync(string workName, string content)
        {
            string jobDic = _logPath + $"/{workName}";
            if (!Directory.Exists(jobDic))
            {
                Directory.CreateDirectory(jobDic);
            }

            string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"); //获取当前系统时间

            string filename = jobDic + $"/{DateTime.Now.ToString("yyyy-MM-dd")}.log"; //用日期对日志文件命名

            // 向日志文件写入内容
            string write_content = $"\n{time}:{content}";

            // 创建或打开日志文件，向日志文件末尾追加记录
            await File.AppendAllTextAsync(filename, write_content);
        }
    }
}
